package com.csun.dsms.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.csun.dsms.comment.config.properties.CsunProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 20:27
 * @description: TODO
 */
@Slf4j
@Component
public class OSSManageUtil {

    @Resource
    private CsunProperties csunProperties;

    /**
     * 上传OSS服务器文件
     *
     * @param file          待上传的文件
     * @param fileName      文件名
     * @param contentType   文件类型
     * @param remotePath    文件相对路径
     *
     * @return  文件绝对路径
     *
     * @throws Exception
     */
    public String uploadFile(File file, String fileName, String contentType, String remotePath)
            throws Exception {

        InputStream content = null;
        content = new FileInputStream(file);

        OSSClient ossClient = new OSSClient(csunProperties.getAliyun().getOss().getEndpoint(),
                                            csunProperties.getAliyun().getOss().getAccessKeyId(),
                                            csunProperties.getAliyun().getOss().getAccessKeySecret());

        String remoteFilePath = remotePath.substring(0, remotePath.length())
                .replaceAll("\\\\", "/") + "/";

        // 创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(content.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(contentType);

        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        log.info("remoteFilePath = " + remoteFilePath);

        // 上传文件
        ossClient.putObject(csunProperties.getAliyun().getOss().getBucketName(), remoteFilePath
                + fileName, content, objectMetadata);

        log.info(csunProperties.getAliyun().getOss().getAccessUrl() + "/" + remoteFilePath
                + fileName);

        return csunProperties.getAliyun().getOss().getAccessUrl() + "/" + remoteFilePath
                + fileName;
    }

    /**
     * 根据key删除OSS服务器上的文件
     *
     * @Title: deleteFile
     * @Description:
     * @param @param ossConfigure
     * @param @param filePath 设定文件
     * @return void 返回类型
     * @throws
     */
    public  void deleteFile(String filePath) {

        OSSClient ossClient = new OSSClient(csunProperties.getAliyun().getOss().getEndpoint(),
                                            csunProperties.getAliyun().getOss().getAccessKeyId(),
                                            csunProperties.getAliyun().getOss().getAccessKeySecret());

        log.info("filePath = " + filePath);

        if (filePath.startsWith(csunProperties.getAliyun().getOss().getAccessUrl() + "/")) {
            String key = filePath.substring(csunProperties.getAliyun().getOss().getAccessUrl()
                    .length() + 1, filePath.length());
            System.out.println("key = " + key);
            ossClient.deleteObject(csunProperties.getAliyun().getOss().getBucketName(), key);
        }

    }

    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @Version1.0
     * @param FilenameExtension
     *            文件后缀
     * @return String
     */
    public static String contentType(String FilenameExtension) {
        if (FilenameExtension.equals("BMP") || FilenameExtension.equals("bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equals("GIF") || FilenameExtension.equals("gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equals("JPEG")
                || FilenameExtension.equals("jpeg")
                || FilenameExtension.equals("JPG")
                || FilenameExtension.equals("jpg")
                || FilenameExtension.equals("PNG")
                || FilenameExtension.equals("png")) {
            return "image/jpeg";
        }
        if (FilenameExtension.equals("HTML")
                || FilenameExtension.equals("html")) {
            return "text/html";
        }
        if (FilenameExtension.equals("TXT") || FilenameExtension.equals("txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equals("VSD") || FilenameExtension.equals("vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equals("PPTX")
                || FilenameExtension.equals("pptx")
                || FilenameExtension.equals("PPT")
                || FilenameExtension.equals("ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equals("DOCX")
                || FilenameExtension.equals("docx")
                || FilenameExtension.equals("DOC")
                || FilenameExtension.equals("doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equals("XML") || FilenameExtension.equals("xml")) {
            return "text/xml";
        }
        if (FilenameExtension.equals("APK") || FilenameExtension.equals("apk")) {
            return "application/vnd.android.package-archive,application/octet-stream";
        }
        if (FilenameExtension.equals("PDF") || FilenameExtension.equals("pdf")) {
            return "application/pdf";
        }
        return "text/html";
    }

}
